<?php

namespace App\Models\User;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class LiveUserCard extends Model
{
    public $table = 'live_user_card';
    public $timestamps = false;

    public function getCreatedTimeAttribute($val)
    {
        return date('Y-m-d',$val);
    }

    public function getUseTimeAttribute($val)
    {
        return $val ? date('Y-m-d',$val) : 0;
    }

    #获取卡密列表
    public static function getCardList($start_time,$end_time,$status,$search)
    {
        $data = self::with(['useUser','agentUser'])
                ->when(!empty($start_time) && !empty($end_time), function ($query) use ($start_time, $end_time) {
                    return $query->whereBetween('created_time', [Carbon::parse($start_time)->startOfDay(), Carbon::parse($end_time)->endOfDay()]);
                })
                ->when($status > -1, function ($query) use ($status) {
                    return $query->where('status','=',$status);
                })
                ->when($search > 0, function ($query) use ($search) {
                    return $query->where('user_id','like',"%{$search}%");
                })
                ->select('id','user_id','use_user_id','code','vip_time','status','created_time','use_time')
                ->orderByDesc('created_time')
                ->paginate(15)
                ->toArray();
        if (empty($data)) {
            return [];
        } else {
            return $data;
        }
    }

    #创建多条卡密记录
    public static function createCard(array $data)
    {
        $flag = self::insert($data);
        return $flag;
    }

    #根据用户id获取未激活的卡密列表
    public static function getCardListByUserId(int $user_id, int $type)
    {
        $sql = self::query()->where('user_id','=',$user_id)->where('status','=',0);
        if ($type == 1) {
            $sql->where('vip_time','=',30);
        } elseif ($type == 2) {
            $sql->where('vip_time','=',90);
        } elseif ($type == 3) {
            $sql->where('vip_time','=',365);
        } else {
            $sql->where('vip_time','=',999999999);
        }
        $data = $sql->orderByDesc('created_time')->paginate(15)->toArray();
        return $data;
    }

    #根据用户id获已激活的卡密列表
    public static function getUseCardByUserId(int $user_id)
    {
        $data = self::with('useUser')->where('user_id','=',$user_id)->where('status','=',1)->orderByDesc('created_time')->paginate(15)->toArray();
        return $data;
    }

    #根据激活码获取指定卡密
    public static function getCardByCode(string $code)
    {
        $data = self::query()->where('code','=',$code)->first();
        return $data;
    }

    #根据激活码激活卡密
    public static function renewCard(string $code, int $user_id)
    {
        $flag = self::query()->where('code','=',$code)->update([
                    'use_user_id'   => $user_id,
                    'status'        => 1,
                    'use_time'      => time(),
                ]);
        return $flag;
    }

    #激活用户关联用户表
    public function useUser()
    {
        return $this->hasOne(LiveUser::class,'id','use_user_id')->select('id','user_name')->withDefault([
            'user_name' => '',
        ]);
    }

    #代理用户关联用户表
    public function agentUser()
    {
        return $this->hasOne(LiveUser::class,'id','user_id')->select('id','user_name')->withDefault([
            'user_name' => '',
        ]);
    }
}
